// pages/address/address.js
const wxapi = require("../../utils/wxapi");
const app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    operateType: null,
    addressChooseId: null,
    addressOfData: null,
    actionType: null,
    defaultAddressId: null
  },

  /**
   * 获取地址列表
   */
  getAddressList() {
    const headers = {
      'content-type': 'application/x-www-form-urlencoded;charset=utf-8',
      Authorization: app.globalData.token
    };
    const bodyparams = {
      cardNo: app.globalData.userInfo.cardNo
    }
    wxapi.getUserAddress(bodyparams, headers).then(res => {
      if (res.code === 20000) {
        this.setData({
          addressOfData: res.data
        })
      } else {
        wx.showModal({
          title: '提示信息',
          content: res.msg,
          showCancel: false
        })
      }
    })
  },

  /**
   * 添加新地址
   */
  editAdress(e) {
    const addressItem = e.currentTarget.dataset.addressItem;
    wx.navigateTo({
      url: `./edit/edit?type=${this.data.operateType}`,
      success: (res) => {
        res.eventChannel.emit('addressMsg', addressItem)
      }
    })
  },

  /**
   * 选择收货地址
   * @param {*} e 
   */
  chooseAddress(e) {
    if (this.data.operateType !== 'select') return;
    const chooseAddress = e.currentTarget.dataset.addressItem;
    const pages = getCurrentPages();
    const prevPage = pages[pages.length - 2];
    prevPage.setData({
      chooseAddress
    })
    wx.navigateBack({
      delta: 1
    })
  },

  /**
   * 管理地址
   */
  editAddress() {
    this.setData({
      actionType: 'edit'
    })
  },

  /**
   * 完成操作
   */
  updateAddress() {
    if (this.data.defaultAddressId) {
      const headers = {
        'content-type': 'application/x-www-form-urlencoded;charset=utf-8',
        Authorization: app.globalData.token
      };
      const bodyparams = {
        cardNo: app.globalData.userInfo.cardNo,
        addressId: this.data.defaultAddressId
      }
      wxapi.editDefaultAddress(bodyparams, headers).then(res => {
        if (res.code === 20000) {
          this.getAddressList();
          this.setData({
            actionType: null
          })
        } else {
          wx.showModal({
            title: '',
            content: res.msg,
            showCancel: false
          })
        }
      })
    } else {
      this.setData({
        actionType: null
      })
    }
  },

  /**
   * 删除地址
   * @param {*} e 
   */
  deleteAddress(e) {
    wx.showModal({
      title: '',
      content: '确定要删除该地址',
      confirmText: '删除',
      complete: (res) => {
        if (res.confirm) {
          const addressId = e.currentTarget.dataset.addressId;
          WXAPI.removeExchange({ id: addressId }).then(res => {
            if (res.code === 0) {
              this.getAddressList();
            }
          })
        }
      }
    })
  },

  /**
   * 修改默认地址
   * @param {*} e 
   */
  radioChange(e) {
    const addressId = e.detail.value;
    this.setData({
      defaultAddressId: addressId
    })
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    if (options.type) {
      this.setData({
        operateType: options.type
      })
    }
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {
    this.getAddressList();
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})